package date;

import java.io.*;
import java.util.List;
import java.util.Map;

import shop.Shop;
import shop.sales.Sales;

public class Date {
	private final int MAX_YEAR = 2013;
	private final int MIN_MONTH = 1;
	private final int MAX_MONTH = 12;
	private final int MIN_DAY = 1;
	private final int MAX_DAY_IS_31 = 31;
	private final int MAX_DAY_IS_30 = 30;
	private final int MAX_DAY_IS_29 = 29;
	private final int MAX_DAY_IS_28 = 28;

	private int month = 0;
	private int year = 0;

	public static void main(String[] args) throws IOException {
		Shop shop = new Shop();
		Date d = new Date();
		Sales sales = new Sales();
		
		Map<String, List<Map<String, String>>> map = sales.getMapOfSales();

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		String rangeDate = null;
		System.out
				.println("Date: yyyy/mm/dd\n<start_date>-<end_date>-<product>\n\"all\" word enter instead <product> "
						+ "if you want to display all sold products\nExample: 2013/6/1-2013/8/31-DVD");

		while ((rangeDate = in.readLine()) != "exit") {
			
			try {
				
				String[] splitRangeOfDate = rangeDate.split("-");
				String[] firstDate = d.getSplitedDate(splitRangeOfDate[0]);
				String[] secondDate = d.getSplitedDate(splitRangeOfDate[1]);
				shop.parseDates(firstDate, secondDate, splitRangeOfDate[2], map);
			} catch (ArrayIndexOutOfBoundsException e) {
				
				System.out.println("Enter correct data who was displayed in example!");
			}
		}

	}

	private String[] getSplitedDate(String date) {
		
		return date.split("/");
	}

	public boolean verifyYear(String yearAsStr) {

		try {
			year = Integer.parseInt(yearAsStr);
			if (yearAsStr.length() == 4) {
				if (MAX_YEAR == year)
					return true;
				else
					System.out.println("This program has limitation by year!\n"
							+ "Would you enter " + MAX_YEAR + " year, please");
			} else
				System.out.println("Year: Would you enter 4 digits, please");

		} catch (NumberFormatException e) {
			System.out.println("Year: You should enter digits!");
			return false;
		}
		return false;
	}

	public boolean verifyMonth(String monthAsStr) {

		try {
			month = Integer.parseInt(monthAsStr);
			if (monthAsStr.length() == 2 || monthAsStr.length() == 1) {
				if (MIN_MONTH <= month && MAX_MONTH >= month)
					return true;
				else if (month != 0)
					System.out.println("Would you enter correct month, please");
			} else
				System.out.println("Month: Would you enter 2 digits, please");

		} catch (NumberFormatException e) {
			System.out.println("Month: You should enter digits!");
			return false;
		}
		return false;
	}

	public boolean verifyDay(String dayAsStr) {
		int day = 0;
		int maxDay = 0;

		try {
			day = Integer.parseInt(dayAsStr);
			maxDay = getMaxDay();
			if (dayAsStr.length() == 2 || dayAsStr.length() == 1) {
				if (MIN_DAY <= day && getMaxDay() >= day)
					return true;
				else if (maxDay != 0)
					System.out.println("This day has " + maxDay + " days\n"
							+ "Would you enter correct day, please");
			} else
				System.out.println("Day: Would you enter 2 digits, please");

		} catch (NumberFormatException e) {
			return false;
		}
		return false;
	}

	private int getMaxDay() {
		int[] MonthsWith31Days = { 1, 3, 5, 7, 8, 10, 12 };
		int[] MonthsWith30Days = { 4, 6, 9, 11 };
		int monthOfLeapYear = 2;
		int maxDay = 0;

		for (int monthWith31Days : MonthsWith31Days)
			if (monthWith31Days == this.month)
				return MAX_DAY_IS_31;

		for (int monthWith30Days : MonthsWith30Days)
			if (monthWith30Days == this.month)
				return MAX_DAY_IS_30;

		if (monthOfLeapYear == this.month) {
			if (0 == year % 2)
				return MAX_DAY_IS_29;
			else
				return MAX_DAY_IS_28;
		}

		return maxDay;

	}

}
